package com.dragon.test.springbootdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;

/**
 * @Author: longtao
 * @Date: 2019/5/31
 * @Version: 1.0
 * @Description:
 */
@Controller
@RequestMapping("/test")
public class GetHttpBodyController {


    @RequestMapping(value = "/getHttpBody")
    @ResponseBody
    public String getHttpBody(HttpServletRequest request/*, @RequestBody Map jsonObject*/) {

        //System.out.println(jsonObject.toString());
        //System.out.println(jsonObject.get("pwd"));


        System.out.println(readAsChars(request));
        //System.out.println(readAsChars2(request));
        //System.out.println(new String(readAsBytes(request)));


        //repeatablRead(request);


        return "ok!";
    }


    /**
     * @param request
     * @return
     */
    public static String readAsChars(HttpServletRequest request) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }


    /**
     * @param request
     */
    public static String readAsChars2(HttpServletRequest request) {
        InputStream is = null;
        StringBuilder sb = new StringBuilder();

        try {
            is = request.getInputStream();
            byte[] b = new byte[4096];
            for (int n; (n = is.read(b)) != -1; ) {
                sb.append(new String(b, 0, n));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();
    }


    /**
     * @param request
     * @return
     */
    public static byte[] readAsBytes(HttpServletRequest request) {
        int len = request.getContentLength();
        byte[] buffer = new byte[len];
        InputStream in = null;

        try {
            in = request.getInputStream();
            in.read(buffer, 0, len);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return buffer;
    }


    /**
     * @param request
     */
    public void repeatablRead(HttpServletRequest request) {
        int len = request.getContentLength();
        System.out.println(len);

        byte[] bytes = new byte[len];
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = request.getInputStream();
            bis = new BufferedInputStream(is);
            bis.mark(len);

            for (int i = 0; i < 10; ++i) {
                bis.reset();
                bis.read(bytes, 0, len);
                System.out.println(i + " : " + new String(bytes));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
